package com.apes.fn.scm.retailOrder.service;

import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.retailOrder.repository.RetailOrderRepository;
import com.apes.fn.scm.retailOrder.service.account.RetailAccountData;
import com.apes.fn.scm.retailOrder.service.account.RetailAccountService;
import com.apes.fn.server.servicePlan.model.ServicePlan;
import com.apes.fn.server.servicePlan.model.ServicePlanItem;
import com.apes.fn.server.servicePlan.repository.ServicePlanRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 零售流程写往来账
 *
 * @author wuq
 * @create 2020-05-12 14:17
 */
@Service
public class RetailOrderFlowInvoiceService extends DomainService {

    @Autowired
    private RetailInsuranceDiscountsService retailInsuranceDiscountsService;
    @Autowired
    private RetailAccountService retailAccountService;

    /**
     * 标准零售场景流调用
     * @param request RetailOrder
     * @return RetailOrder
     */
    public RetailOrder create(SimpleRequest request) {
        RetailOrder retailOrder = request.getO(RetailOrder.class);
        process(retailOrder);
        return retailOrder;
    }

    private void process(RetailOrder retailOrder) {
        if (Objects.isNull(retailOrder.getInsuranceDiscounts()) || "retailReturn".equals(retailOrder.getOrderType()))
            return;

        // e 保养服务处理
        retailInsuranceDiscountsService.process(retailOrder);
    }

    /**
     * 异业合作的部分
     * 1. 异业合作
     * 2. 单位客户挂账部分
     *
     * @param request
     * @return
     */
    public RetailOrder createCrossIC(SimpleRequest request) {
        String id = request.get("id");
        RetailOrder retailOrder = SpringManager.getBean(RetailOrderRepository.class).findOne(id);

        /* 在下面的逻辑中都有判断，如果不满足就 return */
        // 单位客户挂账部分
        retailAccountService.process("unitClient", new RetailAccountData(retailOrder));

        // 异业部分
        retailAccountService.process("crossIc", new RetailAccountData(retailOrder));

        return retailOrder;
    }


}
